//
//  ATTMBiddingDelegate.m
//  HeadBiddingDemo
//
//  Created by lix on 2022/10/20.
//

#import "CJATTMBiddingDelegate.h"
#import "CJATTMBiddingManager.h"
#import "CJATTMBiddingRequest.h"
#import <CJMobileAd/CJMobileAd.h>

@interface CJATTMBiddingDelegate () <CJSplashAdDelegate, CJInterstitialAdDelegate, CJBannerAdDelegate, CJRewardVideoAdDelegate, CJNativeAdDelegate>

@end

@implementation CJATTMBiddingDelegate

#pragma mark splash delegate
- (void)splashAdDidLoad:(nonnull CJSplashAd *)splashAd resourceId:(nonnull NSString *)resourceId {
    NSInteger adnCpm = [splashAd getECpm];
    CJATTMBiddingRequest *request = [[CJATTMBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    if (request.bidCompletion) {
        ATBidInfo *bidInfo = [ATBidInfo bidInfoC2SWithPlacementID:request.placementID unitGroupUnitID:request.unitGroup.unitID adapterClassString:request.unitGroup.adapterClassString price:@(adnCpm/100.0f).stringValue currencyType:ATBiddingCurrencyTypeCNY expirationInterval:request.unitGroup.networkTimeout customObject:splashAd];
        request.bidCompletion(bidInfo, nil);
    }
    
    [[CJATTMBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
    
}

- (void)splashAdLoadFailed:(nonnull CJSplashAd *)splashAd error:(nonnull NSError *)error {
    CJATTMBiddingRequest *request = [[CJATTMBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    if (request.bidCompletion) {
        request.bidCompletion(nil, error);
    }
    [[CJATTMBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
}

#pragma mark interstitial delegate
- (void)interstitialAdDidLoad:(CJInterstitialAd *)interstitialAd resourceId:(NSString *)resourceId {
    NSInteger adnCpm = [interstitialAd getECpm];
    CJATTMBiddingRequest *request = [[CJATTMBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    if (request.bidCompletion) {
        ATBidInfo *bidInfo = [ATBidInfo bidInfoC2SWithPlacementID:request.placementID unitGroupUnitID:request.unitGroup.unitID adapterClassString:request.unitGroup.adapterClassString price:@(adnCpm/100.0f).stringValue currencyType:ATBiddingCurrencyTypeCNY expirationInterval:request.unitGroup.networkTimeout customObject:interstitialAd];
        request.bidCompletion(bidInfo, nil);
    }
    
    [[CJATTMBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
}

- (void)interstitialAdLoadFailed:(CJInterstitialAd *)interstitialAd error:(NSError *)error {
    CJATTMBiddingRequest *request = [[CJATTMBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    if (request.bidCompletion) {
        request.bidCompletion(nil, error);
    }
    [[CJATTMBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
}

#pragma mark banner delegate
- (void)bannerAdDidLoad:(UIView *)bannerView resourceId:(NSString *)resourceId {
    NSLog(@"%s", __FUNCTION__);
    if (self.callbackBannerView) { self.callbackBannerView(bannerView); }
    CJATTMBiddingRequest *request = [[CJATTMBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    if (request.bidCompletion) {
        ATBidInfo *bidInfo = [ATBidInfo bidInfoC2SWithPlacementID:request.placementID unitGroupUnitID:request.unitGroup.unitID adapterClassString:request.unitGroup.adapterClassString price:@(0/100.0f).stringValue currencyType:ATBiddingCurrencyTypeCNY expirationInterval:request.unitGroup.networkTimeout customObject:bannerView];
        request.bidCompletion(bidInfo, nil);
    }
    
    [[CJATTMBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
}

- (void)bannerAdLoadFailed:(id)bannerAd error:(NSError *)error {
    NSLog(@"%s %@", __FUNCTION__ ,error);
    CJATTMBiddingRequest *request = [[CJATTMBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    if (request.bidCompletion) {
        request.bidCompletion(nil, error);
    }
    [[CJATTMBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
}

#pragma mark rewardvodeo delegate
- (void)rewardVideoAdDidLoad:(CJRewardVideoAd *)rewardAd resourceId:(NSString *)resourceId {
    NSInteger adnCpm = [rewardAd getECpm];
    CJATTMBiddingRequest *request = [[CJATTMBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    if (request.bidCompletion) {
        ATBidInfo *bidInfo = [ATBidInfo bidInfoC2SWithPlacementID:request.placementID unitGroupUnitID:request.unitGroup.unitID adapterClassString:request.unitGroup.adapterClassString price:@(adnCpm/100.0f).stringValue currencyType:ATBiddingCurrencyTypeCNY expirationInterval:request.unitGroup.networkTimeout customObject:rewardAd];
        request.bidCompletion(bidInfo, nil);
    }
    [[CJATTMBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
}

- (void)rewardVideoAdLoadFailed:(CJRewardVideoAd *)rewardAd error:(NSError *)error {
    CJATTMBiddingRequest *request = [[CJATTMBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    if (request.bidCompletion) {
        request.bidCompletion(nil, error);
    }
    [[CJATTMBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
}

#pragma mark native delegate
- (void)nativeExpressAdDidLoad:(id)nativeExpressAd views:(NSArray *)nativeExpressViews {
    NSMutableArray<NSDictionary*>* assets = [NSMutableArray<NSDictionary*> array];
    CJATTMBiddingRequest *request = [[CJATTMBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    CJNativeAd *nativeAd = request.customObject;
    NSInteger adnCpm = 0;
    if (nativeAd) {
        adnCpm = [nativeAd getECpm];
    }
    [nativeExpressViews enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableDictionary *asset = [NSMutableDictionary dictionary];
        UIView *expressView = (UIView *)obj;
        [asset setValue:expressView forKey:kATAdAssetsCustomObjectKey];
        // 原生模板广告
        [asset setValue:@(1) forKey:kATNativeADAssetsIsExpressAdKey];
//        katnat
        [asset setValue:@(expressView.frame.size.width) forKey:kATNativeADAssetsNativeExpressAdViewWidthKey];
        [asset setValue:@(expressView.frame.size.height) forKey:kATNativeADAssetsNativeExpressAdViewHeightKey];
        [asset setValue:request.customObject forKey:@"custom-platform-adInstance"];
        [assets addObject:asset];
    }];
    request.assets = assets;
    if (request.bidCompletion) {
        ATBidInfo *info = [ATBidInfo bidInfoC2SWithPlacementID:request.placementID
                                                   unitGroupUnitID:request.unitGroup.unitID
                                                   adapterClassString:request.unitGroup.adapterClassString
                                                   price:@(adnCpm/100.0f).stringValue
                                                   currencyType:ATBiddingCurrencyTypeCNY
                                                   expirationInterval:request.unitGroup.networkTimeout
                                                  customObject:request.customObject];
        request.bidCompletion(info, nil);
    }
    
    [[CJATTMBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
}

- (void)nativeExpressAdLoadFailed:(id)nativeExpressAd error:(NSError *)error {
    CJATTMBiddingRequest *request = [[CJATTMBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    if (request.bidCompletion) {
        request.bidCompletion(nil, error);
    }
    [[CJATTMBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
}
@end
